package com.cws.project.cws.homework.service.impl;

import java.util.List;

import com.cws.common.utils.DateUtils;
import com.cws.common.utils.text.Convert;
import com.cws.project.cws.course.domain.Course;
import com.cws.project.cws.course.mapper.CourseMapper;
import com.cws.project.cws.score.domain.HomeworkScore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cws.project.cws.homework.mapper.HomeworkMapper;
import com.cws.project.cws.homework.domain.Homework;
import com.cws.project.cws.homework.service.IHomeworkService;

/**
 * 作业管理Service业务层处理
 *
 * @author xyl
 * @date 2022-01-10
 */
@Service
public class HomeworkServiceImpl implements IHomeworkService {
    @Autowired
    private HomeworkMapper homeworkMapper;
    @Autowired
    private CourseMapper courseMapper;

    /**
     * 查询作业管理
     *
     * @param homeworkId 作业管理ID
     * @return 作业管理
     */
    @Override
    public Homework selectHomeworkById(Long homeworkId) {
        return homeworkMapper.selectHomeworkById(homeworkId);
    }

    /**
     * 查询作业管理列表
     *
     * @param homework 作业管理
     * @return 作业管理
     */
    @Override
    public List<Homework> selectHomeworkList(Homework homework) {
        return homeworkMapper.selectHomeworkList(homework);
    }

    /**
     * 新增作业管理
     *
     * @param homework 作业管理
     * @return 结果
     */
    @Override
    public int insertHomework(Homework homework) {
        homework.setCreateTime(DateUtils.getNowDate());
        //关联到班级
        Long courseId = homework.getCourseId();
        Course course = courseMapper.selectCourseById(courseId);
        homework.setClazzId(course.getClazzId());
        return homeworkMapper.insertHomework(homework);
    }

    /**
     * 修改作业管理
     *
     * @param homework 作业管理
     * @return 结果
     */
    @Override
    public int updateHomework(Homework homework) {
        homework.setUpdateTime(DateUtils.getNowDate());
        //关联到班级
        Long courseId = homework.getCourseId();
        Course course = courseMapper.selectCourseById(courseId);
        homework.setClazzId(course.getClazzId());
        return homeworkMapper.updateHomework(homework);
    }

    /**
     * 删除作业管理对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteHomeworkByIds(String ids) {
        return homeworkMapper.deleteHomeworkByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除作业管理信息
     *
     * @param homeworkId 作业管理ID
     * @return 结果
     */
    @Override
    public int deleteHomeworkById(Long homeworkId) {
        return homeworkMapper.deleteHomeworkById(homeworkId);
    }

    @Override
    public Homework selectHomeworkByIdAndStudentId(Long homeworkId, Long studentId) {
        return homeworkMapper.selectHomeworkByIdAndStudentId(homeworkId,studentId);
    }
}
